package dieciLuglio;

import java.io.*;
import java.sql.*;
import java.util.*;



public class Utente implements Serializable{

	private Statement stat;
	private String nome;
	private String cognome;
	private String nick;
	private String indirizzo;
	private String dataDiNascita;
	private String pass;
	private int codiceU;
	private Connection conn;
	//private Map<String,LinkedList<Alimento>> piattiPrecedenti;
	
	public Utente(){}
	public Utente(int c){
		codiceU=c;
		String completaUtente="select * from Utenti where codiceU="+c;
		ResultSet rs=null;
		try{
			conn = getConnection();
			stat = conn.createStatement();
			rs=stat.executeQuery(completaUtente);
			while(rs.next()){
				this.nome=rs.getString(2);
				this.cognome=rs.getString(3);
				//this.dataDiNascita=rs.getDate(4);
				this.indirizzo=rs.getString(5);
				this.nick=rs.getString(6);
				this.pass=rs.getString(7);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}catch(IOException ew){
			ew.printStackTrace();
		}
		finally{
			try{
				rs.close();
				stat.close();
				conn.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
	}
	
	public Utente(String nick,String pass){
		this.nick=nick;
		this.pass=pass;
		String completaUtente="select * from Utenti where nick='"+nick+"'";
		ResultSet rs=null;
		try{
			conn = getConnection();
			stat = conn.createStatement();
			rs=stat.executeQuery(completaUtente);
			while(rs.next()){
				this.codiceU=rs.getInt(1);
				this.nome=rs.getString(2);
				this.cognome=rs.getString(3);
				//this.dataDiNascita=rs.getDate(4);
				this.indirizzo=rs.getString(5);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}catch(IOException ew){
			ew.printStackTrace();
		}
		finally{
			try{
				rs.close();
				stat.close();
				conn.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
	}
	
	
	public Utente(String nome,String cognome,String nick,String indirizzo,String dataDiNascita,String pass){
		this.nome=nome;
		this.cognome=cognome;
		this.nick=nick;
		this.indirizzo=indirizzo;
		this.dataDiNascita=dataDiNascita;
		this.pass=pass;
		
	}
	
	public String getNome(){
		return nome;
	}
	public int getCodice(){
		return codiceU;
	}
	
	
	public String getCognome(){
		return cognome;
	}
	public String getDataDiNascita(){
		return dataDiNascita;
	}
	public String getIndirizzo(){
		return indirizzo;
	}
	public String getNick(){
		return nick;
	}
	public String getPass(){
		return pass;
	}
	
	public boolean utenteGiaRegistrato(){
		String v="select * from Utenti where nome='Michele' and cognome='Esposito'";// and dataNascita='"+this.dataDiNascita+"' and indirizzo='"+this.indirizzo+"' and nick='"+this.nick+"'";
		System.out.println(this);
		ResultSet rs =null;
		System.out.println();
		boolean ce=false;
		Utente tizio;
		
		try{
			conn = getConnection();
			stat = conn.createStatement();
			rs = stat.executeQuery("select * from Utenti where nome='Michele' and cognome='Esposito'");
			while(rs.next()){
				String no=rs.getString(2);
				String c=rs.getString(3);
				String dN=(String)rs.getDate(4).toString();
				String i=rs.getString(5);
				String ni=rs.getString(6);
				
				tizio=new Utente(no,c,ni,i,dN,"");
				System.out.println(tizio);
				if(this.nome==tizio.nome){
					System.out.println("PRESENTE");
					ce=true;
				}
				else{
					System.out.println("NON E' possibile");
				}
					
			}
		}catch(IOException d){
			d.printStackTrace();
		}catch(SQLException s){
			s.printStackTrace();
		}
		
		finally{
			try{
				rs.close();
				stat.close();
				conn.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		return ce;
	}

	public void registraUtente(){
		String q="insert into Utenti (nome,cognome,dataNascita,indirizzo,nick,pass) values ('"+this.getNome()+"','"+this.getCognome()+"','"+this.getDataDiNascita()+"','"+this.getIndirizzo()+"','"+this.getNick()+"','"+this.getPass()+"')";
		try{
			conn=getConnection();
			stat=conn.createStatement();
			stat.executeUpdate(q);
		}catch(IOException e){
			e.printStackTrace();
		}
		catch(SQLException e){
			e.printStackTrace();
		}
		finally{
			try{
				stat.close();
				conn.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
	}
	/***
	 * Si chiede al database se esiste una corrispondenza tra nick e pass
	 * se la trova restiruisce vero altrimenti falso
	 * @param nick
	 * @param pass
	 * @return
	 * @throws SQLException
	 */
	public boolean accediUtente(){
		String connetti="select nick,pass from Utenti where nick='"+this.nick+"' and pass='"+this.pass+"'";
		ResultSet rs=null;
		try{
			conn=getConnection();
			stat=conn.createStatement();
			rs=stat.executeQuery(connetti);
			while(rs.next())
				return true;
			}catch (SQLException e){
				e.printStackTrace();
			}catch(IOException e){
				e.printStackTrace();
			}
		finally{
			try{
				rs.close();
				stat.close();
				conn.close();
			}catch(SQLException w){
				w.printStackTrace();
			}
		}
			return false;
	}
	
	
	public String toString(){
		return "codice: "+this.codiceU+" Cognome: "+this.cognome;
	}
	/*
	public Map<String,LinkedList<Alimento>> getListaPiatti(){
		return piattiPrecedenti;
	}
	*/
	/***
	 * DA FINIRE 
	 */
	/*
	public void riempiMap(){
		String q="select data,codiceP from Valutazione where nomeCliente='"+this.nick+"'";
		ResultSet rs=null;
		try{
			conn=getConnection();
			stat=conn.createStatement();
			rs=stat.executeQuery(q);
			while(rs.next()){
				String da=rs.getDate(1).toString();
				int p=rs.getInt(2);
				//if(piattiPrecedenti.containsKey(da)){
					LinkedList<Alimento> tmp=piattiPrecedenti.get(da);
					tmp.addLast(new Alimento(p));
					piattiPrecedenti.put(da, tmp);
				
				}else{
					piattiPrecedenti.put(da, );
				}
			}
		}catch (SQLException e){
			e.printStackTrace();
		}
		catch (IOException e){
			e.printStackTrace();
		}

	}*/

	/*
	public static void main(String []args) throws SQLException{
		Utente u=new Utente("miku92","");
		
		System.out.println(u);
	}
	*/
	
	
	//DA FINIRE!!!!!!!
	public boolean equals(Object x){
		if(!(x instanceof Utente))
			return false;
		if(this==x)
			return true;
		Utente p=(Utente)x;
		return p.nick==this.nick && p.nome==this.nome;
	}
	
	public static Connection getConnection() throws SQLException, IOException{  
		Properties props = new Properties();
		FileInputStream in = new FileInputStream("database.properties");
		props.load(in);
		in.close();

		String drivers = props.getProperty("jdbc.drivers");
		String url = props.getProperty("jdbc.url");
		String username = props.getProperty("jdbc.username");
		String password = props.getProperty("jdbc.password");

		if (drivers != null)
			System.setProperty("jdbc.drivers", drivers);

		return DriverManager.getConnection(url, username, password);
	}
}
